/*
 +-----------------------------------------------------------------------+
 | RoundCube Webmail Client Script										 |
 |																		 |
 | This file is part of the RoundCube Webmail client					 |
 | Copyright (C) 2008-2009, RoundCube Dev, - Switzerland 	             |
 | Licensed under the GNU GPL											 |
 |																		 |
 +-----------------------------------------------------------------------+
 | Author: Philip Weir													 |
 +-----------------------------------------------------------------------+

 $Id: $

*/



function addressrule_del(obj) {
	var adrTable = rcube_find_object('address-rules-table').tBodies[0];
	var rowidx = obj.parentNode.parentNode.rowIndex - 1;
	var fieldidx = rowidx - 1;

	if (!confirm(rcmail.get_label('spamaddressdelete')))
		return false;

	if (document.getElementsByName('_address_rule_act[]')[fieldidx].value == "INSERT") {
		adrTable.deleteRow(rowidx);
	}
	else {
		adrTable.rows[rowidx].style.display = 'none';
		document.getElementsByName('_address_rule_act[]')[fieldidx].value = "DELETE";
	}

	rcmail.env.address_rule_count--;
	if (rcmail.env.address_rule_count < 1)
		adrTable.rows[1].style.display = '';

	return false;
}

function addressrule_add() {
	var adrTable = rcube_find_object('address-rules-table').tBodies[0];
	var input_spamaddressrule = rcube_find_object('_spamaddressrule');
	var input_spamaddresspri = rcube_find_object('_spamaddressrulepri');
	var selrule = input_spamaddressrule.selectedIndex;
	var selpri = input_spamaddresspri.selectedIndex;
	
	var input_spamaddress = rcube_find_object('_spamaddress');

	if (input_spamaddress.value.replace(/^\s+|\s+$/g, '') == '') {
		alert('Please enter and address to add');
		input_spamaddress.focus();
		return false;
	}
	else if ( AmavisCheckEmail(input_spamaddress.value) ) {
		alert(rcmail.get_label('spamaddresserror'));
		input_spamaddress.focus();
		return false;
	}
	else {
		var actions = document.getElementsByName('_address_rule_act[]');
		var prefs = document.getElementsByName('_address_rule_field[]');
		var priority = document.getElementsByName('_address_rule_pri[]');
		var addresses = document.getElementsByName('_address_rule_value[]');
		var insHere;

		for (var i = 1; i < addresses.length; i++){
			if (addresses[i].value == input_spamaddress.value && actions[i].value != "DELETE") {
				alert(rcmail.get_label('spamaddressexists'));
				input_spamaddress.focus();
				return false;
			}
			else if (addresses[i].value > input_spamaddress.value) {
				insHere = adrTable.rows[i + 1];
				break;
			}
		}

		var newNode = adrTable.rows[0].cloneNode(true);
		adrTable.rows[1].style.display = 'none';

		if (insHere)
			adrTable.insertBefore(newNode, insHere);
		else
			adrTable.appendChild(newNode);

		newNode.style.display = "";
		newNode.cells[0].className = input_spamaddressrule.options[selrule].value;
		newNode.cells[0].innerHTML = input_spamaddressrule.options[selrule].text;
		newNode.cells[1].innerHTML = input_spamaddresspri.options[selpri].text;
		newNode.cells[2].innerHTML = input_spamaddress.value;
		
		//set colors
		var Cwhitelist = '#eeffbb';
		var Cblacklist = '#bbbbbb';

		if(input_spamaddressrule.options[selrule].text == 'Whitelist'){
			newNode.style.backgroundColor=Cwhitelist;
		}else if(input_spamaddressrule.options[selrule].text == 'Blacklist'){
			newNode.style.backgroundColor=Cblacklist;
		}
		
		newNode.cells[0].style.fontWeight='bold';
		newNode.cells[1].style.fontWeight='bold';
		newNode.cells[2].style.fontWeight='bold';
		
		
		actions[newNode.rowIndex - 2].value = "INSERT";
		priority[newNode.rowIndex - 2].value = input_spamaddresspri.options[selpri].text;
		prefs[newNode.rowIndex - 2].value = input_spamaddressrule.options[selrule].value;
		addresses[newNode.rowIndex - 2].value = input_spamaddress.value;

		input_spamaddressrule.selectedIndex = 0;
		input_spamaddress.value = '';

		rcmail.env.address_rule_count++;
	}
}

function policy_set(setid){
	var taglvl = rcube_find_object('rcmfd_and-taglvl');
	var killlvl = rcube_find_object('rcmfd_and-killlvl');
	var policyid = rcube_find_object('_policyid');
	var setcheck = false;
	var btnhref_c = rcube_find_object('_policy_ahref[5000]');

		
	var set_taglvl = rcube_find_object('_policy_tag['+setid+']');
	var set_killlvl = rcube_find_object('_policy_kill['+setid+']');
	
	for (var i = 1; i < 8; i++){
		var btnhref = rcube_find_object('_policy_ahref['+i+']');
		
		btnhref.innerHTML = '';
	  if(i == setid){
	  	btnhref.innerHTML = '<img border="0" alt="Set Active" src="plugins/amavisdnewsql/enabled.png"/>';
	  	setcheck = true;
	  }
	}
	btnhref_c.innerHTML = '';
	
	if(setid == 5000 || setcheck == false){
		btnhref_c.innerHTML = '<img border="0" alt="Set Active" src="plugins/amavisdnewsql/enabled.png"/>';
		policyid.value = setid;
		taglvl.value = 5;
		killlvl.value = 6;
		taglvl.focus();
	}else{
		policyid.value = setid;
		taglvl.value = set_taglvl.value;
		killlvl.value = set_killlvl.value;
	}

	return true;
}

function AmavisCheckEmail(emailstr) {

	AtPos = emailstr.indexOf("@")
	StopPos = emailstr.lastIndexOf(".")
	
	if (StopPos == -1) {
		return true;
	}else{
		return false;
	}
}